{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The `Selection` Widgets\n",
    "\n",
    "+ Dropdown\n",
    "+ RadioButtons\n",
    "+ ToggleButtons\n",
    "+ Select\n",
    "+ SelectMultiple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These widgets can be used to choose between multiple alternatives. The `SelectMultiple` widget allows multiple selections, whereas `Dropdown`, `RadioButtons`, `ToggleButtons`, and `Select` only allow one selection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "{-# LANGUAGE OverloadedStrings #-}\n",
    "import IHaskell.Display.Widgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "-- Allows single selection\n",
    "tgbs <- mkToggleButtons\n",
    "\n",
    "-- Allows multiple selections\n",
    "msel <- mkSelectMultiple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "setField msel Description \"Functions to show (One or more)\"\n",
    "setField msel Options (OptionLabels [\"sin\", \"cos\"])\n",
    "\n",
    "setField tgbs Description \"Plot style\"\n",
    "setField tgbs Options (OptionLabels [\"line\", \"point\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The cell below requires `Chart` and `Chart-cairo` to be installed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import Graphics.Rendering.Chart.Easy hiding (tan)\n",
    "import Graphics.Rendering.Chart.Backend.Cairo\n",
    "import qualified Data.ByteString as B\n",
    "import Data.Text (pack, unpack)\n",
    "import IHaskell.Display (base64)\n",
    "import Control.Applicative ((<$>))\n",
    "\n",
    "import Control.Monad (when, forM)\n",
    "import Data.Maybe (fromJust)\n",
    "\n",
    "dset :: [(String, [(Double, Double)])]\n",
    "dset = [(\"sin\", zmap sin r), (\"cos\", zmap cos r)]\n",
    "  where zmap f xs = zip xs (map f xs)\n",
    "        r = [0, 0.1 .. 6.3]\n",
    "\n",
    "i <- mkImageWidget\n",
    "setField i Width 500\n",
    "setField i Height 500\n",
    "\n",
    "-- Redraw the plot based on values from the widgets\n",
    "refresh = do\n",
    "  -- Read values from the widgets\n",
    "  funs <- map unpack <$> getField msel SelectedValues\n",
    "  sty <- unpack <$> getField tgbs SelectedValue\n",
    "  \n",
    "  let pts = zip funs (map (fromJust . flip lookup dset) funs)\n",
    "      opts = def { _fo_size = (500, 500) }\n",
    "  toFile opts \".chart\" $ do\n",
    "    layout_title .= \"Plotting: \" ++ unwords funs\n",
    "    if sty == \"line\"\n",
    "      then mapM_ (\\(s, ps) -> plot (line s [ps])) pts\n",
    "      else mapM_ (\\(s, ps) -> plot (points s ps)) pts\n",
    "\n",
    "  img <- B.readFile \".chart\"\n",
    "  setField i B64Value (base64 img)\n",
    "  \n",
    "-- Add event handlers to make widgets work\n",
    "setField msel SelectionHandler refresh\n",
    "setField tgbs SelectionHandler refresh\n",
    "\n",
    "-- Trigger event to show empty grid initially\n",
    "triggerSelection msel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "-- Display the widgets\n",
    "msel\n",
    "tgbs\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Dropdown`, `RadioButtons` and `Select` widgets behave just like the `ToggleButtons` widget. They have the same properties, and the same functionality."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Haskell",
   "language": "haskell",
   "name": "haskell"
  },
  "language_info": {
   "codemirror_mode": "ihaskell",
   "file_extension": ".hs",
   "name": "haskell",
   "version": "7.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
